﻿using Entity.Models.dto;
using Entity.Models.Entities;
using IBLL;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using utils;
using utils.enums;
using utils.EX;
using Utils;

namespace RepositorySys.Controllers
{
    /// <summary>
    /// 
    /// </summary>
    public class WorkFlow_ModelController : Controller
    {
      
        //工作流模板信息接口
        IWorkFlow_ModelBLL _workFlow_ModelBLL;

        //用户接口
        IUserBLL _userBLL;

        //工作流模板分类
        ICategoryBLL _categoryBLL;

        public WorkFlow_ModelController(IWorkFlow_ModelBLL workFlow_ModelBLL, IUserBLL userBLL, ICategoryBLL categoryBLL)
        {
            _workFlow_ModelBLL = workFlow_ModelBLL;
            _userBLL = userBLL;
            _categoryBLL = categoryBLL;
        }

        #region 视图

        /// <summary>
        /// 工作流模板信息列表
        /// </summary>
        /// <returns></returns>
        public IActionResult Index()
        {
            return View();
        }

        /// <summary>
        /// 添加视图
        /// </summary>
        /// <returns></returns>
        public IActionResult AddWorkFlow_ModelView()
        {
            return View();
        }

        /// <summary>
        /// 修改视图
        /// </summary>
        /// <returns></returns>
        public IActionResult UpdataWorkFlow_ModelView()
        {
            return View();
        }
        #endregion



        /// <summary>
        /// 工作流模板信息列表、搜索
        /// </summary>
        /// <param name="title"></param>
        /// <param name="page"></param>
        /// <param name="limit"></param>
        /// <returns></returns>
        [HttpGet]
        public IActionResult getWorkFlow_ModelList(string title, string page, string limit)
        {
            List<WorkFlow_ModelDTO> workFlow_Model = null;
            int cont = 0;
            try
            {
                workFlow_Model = _workFlow_ModelBLL.getworkFlow_ModelList(title, page, limit, out cont);
            }
            catch (MyException e)
            {
                //获取异常信息
                return getException(workFlow_Model, e);
            }
            return Json(new JsonResults(0, Enums.getDescribe(SettingEnums.Json_STATE.SUCCESS), cont, workFlow_Model));
        }



        /// <summary>
        /// 添加工作流模板信息
        /// </summary>
        /// <param name="workFlow_Model">工作流模板信息信息</param>
        /// <returns></returns>
        [HttpPut]
        public IActionResult AddWorkFlow_Model(WorkFlow_Model workFlow_Model)
        {
            //状态信息
            string mgs;
            object data = null;
            if (Verify(workFlow_Model, out mgs))
            {
                try
                {
                    data = _workFlow_ModelBLL.AddWorkFlow_Model(workFlow_Model);
                }
                catch (MyException e)
                {
                    return getException(data, e);
                }
            }
            return Json(new JsonResults(200, string.IsNullOrEmpty(mgs) ? Enums.getDescribe(UserEnums.USER_STATE.SUCCESS) : mgs, data));
        }


        /// <summary>
        /// 删除工作流模板信息
        /// </summary>
        /// <param name="workFlow_ModelId"></param>
        /// <returns></returns>
        [HttpDelete]
        public IActionResult DalateWorkFlow_Model(string workFlow_ModelId)
        {
            object data = null;
            try
            {
                data = _workFlow_ModelBLL.DalateWorkFlow_Model(workFlow_ModelId);
            }
            catch (MyException e)
            {
                return getException(data, e);
            }
            return Json(new JsonResults(200, Enums.getDescribe(UserEnums.USER_STATE.DATATE), data));
        }


        /// <summary>
        /// 批量删除
        /// </summary>
        /// <param name="workFlow_ModelIds"></param>
        /// <returns></returns>
        [HttpDelete]
        public IActionResult DalateWorkFlow_ModelList(List<string> workFlow_ModelIds)
        {
            object data = null;
            try
            {
                data = _workFlow_ModelBLL.DalateWorkFlow_ModelList(workFlow_ModelIds);
            }
            catch (MyException e)
            {
                return getException(data, e);
            }
            return Json(new JsonResults(200, Enums.getDescribe(UserEnums.USER_STATE.DATATE), data));
        }

        /// <summary>
        /// 修改工作流模板信息信息
        /// </summary>
        /// <param name="workFlow_Model"></param>
        /// <returns></returns>
        public IActionResult UpdataWorkFlow_Model(WorkFlow_Model workFlow_Model)
        {
            object data = null;
            string mgs = null;
            if (Verify(workFlow_Model, out mgs))
            {
                try
                {
                    data = _workFlow_ModelBLL.UpdataWorkFlow_Model(workFlow_Model);
                }
                catch (MyException e)
                {
                    return getException(data, e);
                }
            }
            string a = string.IsNullOrEmpty(mgs) ? Enums.getDescribe(UserEnums.USER_STATE.UPDATE) : mgs;
            return Json(new JsonResults(200, a, data));
        }

        /// <summary>
        /// 根据工作流模板信息id搜索(修改页面数据列表)
        /// </summary>
        /// <param name="workFlow_Modelid">工作流模板信息id</param>
        /// <returns></returns>
        [HttpGet]
        public IActionResult getByWorkFlow_Model(string workFlow_Modelid)
        {
          
            return Json(new JsonResults(200, Enums.getDescribe(SettingEnums.Json_STATE.SUCCESS), _workFlow_ModelBLL.getByWorkFlow_Model(workFlow_Modelid)));
        }


        /// <summary>
        /// 参数验证
        /// </summary>
        /// <param name="workFlow_Model">参数</param>
        /// <returns></returns>
        private bool Verify(WorkFlow_Model workFlow_Model, out string mgs)
        {
            //if (String.IsNullOrEmpty(workFlow_Model.ConsumableName))
            //{
            //    mgs = "工作流模板信息名称不能为空";
            //    return false;
            //}
            //else if (workFlow_Model.LeaderId.EndsWith("0"))
            //{
            //    mgs = "主管不能为空";
            //    return false;
            //}
            //else if (String.IsNullOrEmpty(workFlow_Model.ParentId))
            //{
            //    mgs = "父工作流模板信息不能为空";
            //    return false;
            //}
            mgs = null;
            return true;
        }

        /// <summary>
        /// 获取异常
        /// </summary>
        /// <param name="data"></param>
        /// <param name="e"></param>
        /// <returns></returns>
        private IActionResult getException(object data, MyException e)
        {
            var tt = e.GetExceptionResult();
            return Json(new JsonResults(tt.resultStatus, tt.resultMsg, data));
        }
    }
}
